/**
 * @author Skile
 */
var Pin = new Class({
	Implements: Options,
	options: {
		draggable: false,
		x: 'left',
		y: 'top'
	},
	
	initialize: function(el, options)
	{
		this.Target = $(el);
		
		this.setOptions(options);
		
		this.Target.store('pinner', this);
		
		this.IsPinned = false;
		
		this.PinX = 0;
		this.PinY = 0;
		
		this.PinHandler = 0;
		
		this.InDrag = false;
		
		this.Dragger = null;
		
		if (this.options.draggable) 
		{
			this.Dragger = this.Target.makeDraggable();
			this.Dragger.addEvent('start', this.dragStart.bind(this));
			this.Dragger.addEvent('complete', this.dragEnd.bind(this));
		}
	},
	
	dragStart: function()
	{
		this.InDrag = true;
	},
	
	dragEnd: function()
	{
		if (Browser.Engine.trident4) 
		{
			var coord = this.Target.getCoordinates();
			
			var scroll = window.getScroll();
			
			this.PinX = coord.left - scroll.x;
			this.PinY = coord.top - scroll.y;
		}
		
		this.InDrag = false;
	},
	
	pin: function()
	{
		if (this.IsPinned) 
		{
			return;
		}
		
		var orgPos = this.Target.getStyle('position').toLowerCase();
		
		var coord = this.Target.getCoordinates();
		
		var scroll = window.getScroll();
		
		this.PinX = coord.left - scroll.x;
		this.PinY = coord.top - scroll.y;
		if (Browser.Engine.trident4) 
		{
			this.Target.setStyle('position', 'absolute');
			this.PinHandler = this.keepPos.periodical(50, this);
		}
		else 
		{
			if (orgPos == 'fixed') 
			{
				this.PinX = coord.left;
				this.PinY = coord.top;
			}
			this.Target.setStyles({
				'position': 'fixed',
				'left': this.PinX + 'px',
				'top': this.PinY + 'px'
			});
		}
		
		this.IsPinned = true;
	},
	
	keepPos: function()
	{
		if (this.InDrag) return;
		
		var scroll = window.getScroll();
		
		this.Target.setStyles({
			'left': scroll.x + this.PinX + 'px',
			'top': scroll.y + this.PinY + 'px'
		});
	},
	
	unpin: function()
	{
		if (!this.IsPinned) 
		{
			return;
		}
		
		var coord = this.Target.getCoordinates();
		
		var scroll = window.getScroll();
		
		if (Browser.Engine.trident4) 
		{
			$clear(this.PinHandler);
		}
		else 
		{
			this.Target.setStyles({
				'position': 'absolute',
				'left': coord.left + scroll.x + 'px',
				'top': coord.top + scroll.y + 'px'
			});
		}
		
		this.IsPinned = false;
	},
	
	toggle: function()
	{
		if (this.IsPinned) 
		{
			this.unpin();
		}
		else 
		{
			this.pin();
		}
	}
});

Element.implement({
	pin: function(options)
	{
		var el = $(this);
		var pinner = el.retrieve('pinner', null);
		if (pinner == null) 
		{
			pinner = new Pin(el, options);
		}
		pinner.pin();
		return pinner;
	}
});

Element.implement({
	unpin: function(options)
	{
		var el = $(this);
		var pinner = el.retrieve('pinner', null);
		if (pinner == null) 
		{
			pinner = new Pin(el, options);
		}
		pinner.unpin();
		return pinner;
	}
});

Element.implement({
	togglePin: function(options)
	{
		var el = $(this);
		var pinner = el.retrieve('pinner', null);
		if (pinner == null) 
		{
			pinner = new Pin(el, options);
		}
		pinner.toggle();
		return pinner;
	}
});
